package org.jypj.dev.util;

import org.jypj.dev.vo.SeasonVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    public static String getNextMonth(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        Date time = cal.getTime();
        return sdf.format(time);
    }

    // 取时间差(单位:分)
    public static int getDateOffset(String endDate, String firstDate) {
        int mins = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long endTime = sdf.parse(endDate).getTime();
            long beforeTime = sdf.parse(firstDate).getTime();
            long diff = endTime - beforeTime;
            mins = (int) (diff / (1000 * 60));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return mins;
    }

    // 取时间差(单位:分)
    public static int getDateDayOffset(String endDate, String firstDate) {
        int mins = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            long endTime = sdf.parse(endDate).getTime();
            long beforeTime = sdf.parse(firstDate).getTime();
            long diff = endTime - beforeTime;
            mins = (int) (diff / (1000 * 60 * 60 * 24));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return mins;
    }

    /**
     * 得到今天的日期
     *
     * @param pattern
     * @return
     */
    public static String getTodayString(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        String format = sdf.format(today);
        return format;
    }

    /**
     * 得到今天的特殊日期
     *
     * @param pattern
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getTodayString2(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        if (today.getHours() < 2) {
            long time = today.getTime() - 24 * 60 * 60 * 1000;
            today.setTime(time);
        }
        String format = sdf.format(today);
        return format;
    }

    /**
     * 得到明天的日期
     *
     * @param pattern
     * @return
     */
    public static String getTomorrowString(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        // 通过秒获取下一天日期
        long time = (today.getTime() / 1000) + 60 * 60 * 24;// 秒
        today.setTime(time * 1000);// 毫秒
        String tomorrow = sdf.format(today);
        return tomorrow;
    }

    /**
     * 得到某一天的前几天或者后几天
     *
     * @param pattern
     * @param days    正数表示后几天，负数表示后几天
     * @return
     */
    public static String getSomeDayString(String dateStr, String pattern, int days) {
        String tomorrow = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(dateStr);
            // 通过秒获取下一天日期
            long time = (date.getTime() / 1000) + days * 60 * 60 * 24;// 秒
            date.setTime(time * 1000);// 毫秒
            tomorrow = sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return tomorrow;
    }

    /**
     * 得到昨天的的日期
     *
     * @param pattern
     * @return
     */
    public static String getYestodayString(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        // 通过秒获取前一天的日期
        long time = (today.getTime() / 1000) - 60 * 60 * 24;// 秒
        today.setTime(time * 1000);// 毫秒
        String tomorrow = sdf.format(today);
        return tomorrow;
    }

    /**
     * 得到昨天的的日期
     *
     * @param pattern
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getYestodayString2(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        int day = 1;
        Date today = new Date();
        int hour = today.getHours();
        if (hour < 2) {
            day = 2;
        }
        // 通过秒获取前一天的日期
        long time = (today.getTime() / 1000) - day * 60 * 60 * 24;// 秒
        today.setTime(time * 1000);// 毫秒
        String tomorrow = sdf.format(today);
        return tomorrow;
    }

    /**
     * 得到某一天的第二天
     *
     * @param pattern
     * @return
     */
    public static String getTomorrowStringForSomeDay(String pattern, String day) {
        String tomorrow = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date today = sdf.parse(day);
            // 通过秒获取下一天日期
            long time = (today.getTime() / 1000) + 60 * 60 * 24;// 秒
            today.setTime(time * 1000);// 毫秒
            tomorrow = sdf.format(today);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return tomorrow;
    }

    // 比较日期大小
    public static int compareDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            // date1 > date2
            return 1;
        } else if (date1.getTime() < date2.getTime()) {
            // date1 < date2
            return -1;
        } else {
            // date1 == date2
            return 0;
        }
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param date 当前时间 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static boolean isInDate(Date date, String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        String currentTime = format.substring(11, format.length());
        if (currentTime.compareTo(startTime) >= 0 && currentTime.compareTo(endTime) <= 0) {
            return true;
        }
        return false;
    }

    /**
     * 从时间得到秒数 0-2:00:00特殊计算如果
     *
     * @return
     */
    public static int getSecondFromTime(String time1) {
        int second = 0;
        String time = time1.substring(time1.length() - 8);
        if (StringUtil.isNotEmpty(time) && !time.startsWith("null")) {
            String[] split = time.split(":");
            second = Integer.parseInt(split[0]) * 3600 + Integer.parseInt(split[1]) * 60 + Integer.parseInt(split[2]);
            if (second < 7200) {
                second += 24 * 60 * 60;
            }
        }
        return second;
    }

    /**
     * 得到几天前零点的时间戳 负数表示几天后的
     *
     * @param days
     * @return
     */
    public static String get0StrTimeForDays(int days, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        long currentTime = new Date().getTime();
        long fourDaysBeforeTime = currentTime - (currentTime % (24 * 60 * 60 * 1000)) - days * 24 * 60 * 60 * 1000
                - 8 * 60 * 60 * 1000;
        String format = sdf.format(new Date(fourDaysBeforeTime));
        return format;
    }

    /**
     * 得到几天前零点的毫秒值 负数表示几天后的
     *
     * @param days
     * @return
     */
    public static long get0LongTimeForDays(int days, String pattern) {
        long currentTime = new Date().getTime();
        long fourDaysBeforeTime = currentTime - (currentTime % (24 * 60 * 60 * 1000)) - days * 24 * 60 * 60 * 1000
                - 8 * 60 * 60 * 1000;
        return fourDaysBeforeTime;
    }

    /**
     * 得到某个时间段中每天的日期
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static String[] getEveryDateStrFromDateSpan(String startDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dates = new ArrayList<>();
        try {
            Date startD = sdf.parse(startDate);
            Date endD = sdf.parse(endDate);
            long startTime = startD.getTime();
            long endTime = endD.getTime();
            long tempTime = startTime;
            String tempDateStr = "";
            while (tempTime <= endTime) {
                Date tempDate = new Date(tempTime);
                tempDateStr = sdf.format(tempDate).substring(5);
                dates.add(tempDateStr);
                tempTime += 24 * 60 * 60 * 1000;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String[] a = new String[dates.size()];
        return dates.toArray(a);
    }

    /**
     * 得到某一时间段的月份信息
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static String[] getEveryMonthStrFromDateSpan(String startDate, String endDate) {
        List<String> dates = new ArrayList<>();
        try {
            Date d1 = new SimpleDateFormat("yyyy-MM").parse(startDate); //定义起始日期

            Date d2 = new SimpleDateFormat("yyyy-MM").parse(endDate);//定义结束日期

            Calendar dd = Calendar.getInstance();//定义日期实例

            dd.setTime(d1);//设置日期起始时间

            while (dd.getTime().compareTo(d2) <= 0) {//判断是否到结束日期

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

                String str = sdf.format(dd.getTime());

                dd.add(Calendar.MONTH, 1);//进行当前日期月份加1

                dates.add(str);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String[] a = new String[dates.size()];
        return dates.toArray(a);
    }

    /**
     * 根据月份得到季度信息
     *
     * @param month
     * @return
     */
    public static SeasonVo getSeasonFromMonth(String month) {
        SeasonVo s = new SeasonVo();
        String monthStr = month.substring(5);
        String yearStr = month.substring(0, 4);
        String season = "";
        int num = 0;
        switch (monthStr) {
            case "01":
            case "02":
            case "03":
                season = "第一季度";
                num = 1;
                break;
            case "04":
            case "05":
            case "06":
                season = "第二季度";
                num = 2;
                break;
            case "07":
            case "08":
            case "09":
                season = "第三季度";
                num = 3;
                break;
            case "10":
            case "11":
            case "12":
                season = "第四季度";
                num = 4;
                break;
        }
        s.setYearStr(yearStr);
        s.setName(season);
        s.setNum(num);

        return s;
    }

    /**
     * 根据季度编号得到开始日期和结束日期
     *
     * @param seasonVo
     * @return
     */
    public static Map<String, String> getMonthSpanFromSeason(SeasonVo seasonVo) {
        Map<String, String> dataMap = new HashMap<>();
        String yearStr = seasonVo.getYearStr();
        String startDate = yearStr;
        String endDate = yearStr;
        switch (seasonVo.getNum()) {
            case 1:
                startDate += "-01";
                endDate += "-03";
                break;
            case 2:
                startDate += "-04";
                endDate += "-06";
                break;
            case 3:
                startDate += "-07";
                endDate += "-09";
                break;
            case 4:
                startDate += "-10";
                endDate += "-12";
                break;
        }
        dataMap.put("startDate", startDate);
        dataMap.put("endDate", endDate);
        return dataMap;
    }

    /**
     * 根据时间段得到年份信息
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static String[] getYearSpanFromDateSpan(String startDate, String endDate) {
        int startYear = Integer.parseInt(startDate.substring(0, 4));
        int endYear = Integer.parseInt(endDate.substring(0, 4));
        String[] yearStrs = new String[endYear - startYear + 1];
        int j = 0;
        for (int i = startYear; i <= endYear; i++) {
            yearStrs[j] = i + "";
            j++;
        }
        return yearStrs;
    }

    /**
     * 得到某个时间点的前几个小时
     *
     * @param beforeTime
     * @param hours
     * @return
     */
    public static String getBeforeTime(String beforeTime, double hours) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String year = "2017-01-02 ";
        String time = year + beforeTime;
        Date parse = null;
        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = (long) (parse.getTime() - hours * 60 * 60 * 1000);
        String format = sdf.format(new Date(l));
        return format.substring(11);
    }


    public static void main(String[] args) {
        System.out.println(getBeforeTime("10:38", 11));
    }

}
